package com.xjm.api.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjm.entity.model.OrderItemsModel;
import com.xjm.entity.model.OrdersModel;
import com.xjm.entity.vo.PageVo;
import com.xjm.entity.vo.Result;
import com.xjm.service.IOrderItemsService;
import com.xjm.service.IOrdersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xjm
 * @since 2025-03-03
 */
@RestController
@RequestMapping("/admin/order")
@RequiredArgsConstructor
public class OrdersController {

    private final IOrdersService ordersService;

    private final IOrderItemsService orderItemsService;

    @GetMapping
    public Result<PageVo<OrdersModel>> list(
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "5") Long size
    ) {
        Page<OrdersModel> result = ordersService.page(new Page<>(page, size));
        result.getRecords().forEach(item -> {
            LambdaQueryWrapper<OrderItemsModel> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(OrderItemsModel::getOrderId, item.getId());
            item.setOrderItems(orderItemsService.list(wrapper));
        });
        return Result.success(result);
    }

    // 发货
    @PutMapping
    public Result<Void> send(@RequestBody OrdersModel ordersModel) {
        // 物流单号
        String logisticsNo = ordersModel.getLogisticsNo();
        // 订单id
        Integer id = ordersModel.getId();
        ordersService.send(id, logisticsNo);
        return Result.success();
    }
}
